package com.cloudcc.mobile.view.market;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.market.MarketActivityEntity;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketCheckPhoneNumberEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketCheckResultEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketEventInfoEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketRefreshInfo;
import com.cloudcc.mobile.presenter.MarketActivityPresenter;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.base.BaseFragment;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;
import com.zbar.lib.CaptureActivity;

/** 
* @Description: TODO() 
* @author wangzhiqiang
* @date 2016-4-22 上午10:59:54 
*  
*/
public class MarketActivityInfoFragment extends BaseFragment implements
		IEventLife, OnTitleBarClickListener {

	@Bind(R.id.market_et_phonenumber)
	TextView market_et_phonenumber;
	@Bind(R.id.market_tv_won)
	TextView tvWho;
	@Bind(R.id.market_tv_reqnum)
	TextView market_tv_reqnum;//要求签到人数
	@Bind(R.id.market_tv_has_pq)
	TextView market_tv_has_pq;//已经签到人数
	@Bind(R.id.market_btn_verification)
	Button market_btn_verification;
	@Bind(R.id.market_btn_qrcode)
	Button market_btn_qrcode;
	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;

	private MarketActivityPresenter mMarketPresenter = new MarketActivityPresenter();

	private MarketActivityEntity mMarketActivityInfo;
	private String mMarketActivityId;

	@Override
	public void init() {
		initHeader();
		register();
		refreshInfoData();
	}

	/**
	 * 初始化标题
	 */
	private void initHeader() {
		String title = getArguments().getString(ExtraConstant.EXTRA_NAME, "");
		mMarketActivityId = getArguments().getString(ExtraConstant.EXTRA_ID, "");
		headerbar.setTitle(title);
		headerbar.setOnTitleBarClickListener(this);
		
	}

	/**
	 * 刷新info 数据
	 */
	private void refreshInfoData() {
		showWainting("请稍等...");
		headerbar.beginProgress();
		mMarketPresenter.queryMarketActivityInfo(mMarketActivityId);
		
		
	}
	@OnClick(R.id.market_btn_qrcode)
	public void onClickQrCode() {
		Intent intent = new Intent(mContext, CaptureActivity.class);
		startActivityForResult(intent, 10001);
	}
	@OnClick(R.id.market_btn_verification)
	public void onClickVerification() {//验证 手机号码
		showWainting("正在验证手机号码...");
		mMarketPresenter.valPhoneNumber(market_et_phonenumber.getText().toString().trim(),mMarketActivityId);
	
	}
	
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 10001 && resultCode == Activity.RESULT_OK) {
			String result1 = data.getStringExtra("result");
			checkIn(result1);
		}
		
	}
	
	private class QrCodeModel{
		public String phone;
		public String campaignmemberid;
	}
	
	private void checkIn(String jsonStr){
		QrCodeModel bean = GsonUtil.json2Bean(jsonStr, QrCodeModel.class);
		showWainting("请稍等,正在签到...");
		if (null==bean) {
			dismissWainting();
			showToast("格式不正确");
			return;
		}
		mMarketPresenter.checkByQrcode(bean.campaignmemberid, bean.phone, mMarketActivityId);
	}
	
	public void onEventMainThread(MarketCheckResultEvent event) {
		dismissWainting();
		headerbar.completeProgress();
		if (event.isError()) {
			Toast.makeText(mContext, "亲,签到失败~~", 0).show();
			return;
		}
		showToast("签到成功...");
		//刷新市场活动详情
		MarketRefreshInfo marketRefreshInfo = new MarketRefreshInfo();
		marketRefreshInfo.setOk(true);
		EventEngine.post(marketRefreshInfo);
	}

	public void onEventMainThread(MarketEventInfoEvent event) {
		dismissWainting();
		headerbar.completeProgress();
		if (event.isError()) {
			Toast.makeText(mContext, "亲,获取详情失败~~", 0).show();
			getActivity().finish();
			return;
		}
		mMarketActivityInfo =event.getData().get(0);
		fillDetails();
	}
	/** 验证手机号码
	 * @param event
	 * 2016-4-5
	 */
	public void onEventMainThread(MarketCheckPhoneNumberEvent event) {
		dismissWainting();
		headerbar.completeProgress();
		if (event.isError()||event.getData()==null) {
			Toast.makeText(mContext,event.getMessage(), 0).show();
			return;
		}
		Intent intent =new Intent(mContext, MarketCheckPhoneActivity.class);
		intent.putExtra(ExtraConstant.EXTRA_ID, mMarketActivityId);
		intent.putExtra("market.phone",market_et_phonenumber.getText().toString().trim());
		intent.putExtra("market.name",event.getData().get(0).name);
		startActivity(intent);
	}
	/**刷新
	 * @param event
	 * 2016-4-22
	 */
	public void onEventMainThread(MarketRefreshInfo event) {
		mMarketPresenter.queryMarketActivityInfo(mMarketActivityId);
	}

	/**
	 * 填充详情
	 */
	private void fillDetails() {
		ViewUtils.setText(market_tv_reqnum,covertString( mMarketActivityInfo.getYqhyrs()));
		ViewUtils.setText(market_tv_has_pq,covertString( mMarketActivityInfo.getYqdrs()));
		ViewUtils.setText(tvWho, "所有人:"+String.valueOf(mMarketActivityInfo.getOwneridccname()));
	}

	private String covertString(String aa){
		if (TextUtils.isEmpty(aa)) {
			return String.valueOf(0);
		}
		String remove = StringUtils.remove(aa, ".00");
		return remove;
	}

	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}

	@Override
	public void onClickRight(View v) {

	}

	@Override
	public int getLayoutId() {
		return R.layout.marketactivity_info_frag;
	}

	@Override
	public void register() {
		EventEngine.register(this);
	}

	@Override
	public void onDestroy() {
		EventEngine.uregister(this);
		super.onDestroy();
	}

	@Override
	public void unRegister() {
		EventEngine.uregister(this);
	}
}
